package com.ubao.activity.main;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ubao.R;
import com.ubao.activity.AddFriendActivity;
import com.ubao.activity.MyProductActivity;
import com.ubao.activity.PostProductActivity;
import com.ubao.activity.MyApp.GoBack;
import com.ubao.activity.MyProfileActivity;
import com.ubao.activity.ProductDetailActivity;
import com.ubao.base.RequestBaseActivity;
import com.ubao.bean.ProductInfo;
import com.ubao.global.GoBackable;
import com.ubao.network.ResponseInterface;
import com.ubao.network.Url;
import com.ubao.network.req.SpecialRequest;
import com.ubao.network.resp.SpecialResponse;
import com.ubao.ui.bottommenu.BottomMenu;
import com.ubao.ui.slidingmenu.MyHorizontalScrollView;
import com.ubao.ui.titlebar.GTitleBar;

/**
 * This example uses a FrameLayout to display a menu View and a HorizontalScrollView (HSV).
 * 
 * The HSV has a transparent View as the first child, which means the menu will show through when the HSV is scrolled.
 */
public class HorzScrollWithImageMenu extends RequestBaseActivity implements OnClickListener,GoBackable{
    MyHorizontalScrollView scrollView;
    View menu;
    View app;
    ImageView btnSlide;
    boolean menuOut = false;
    Handler handler = new Handler();
    int btnWidth;
    GTitleBar mTitleBar;
    BottomMenu mBottomMenu;
    private int[] bmenu_ids = {R.id.bmenu_leftButton,R.id.bmenu_middleButton,R.id.bmenu_rightButton};
    private int[] bmenu_drawables = {R.drawable.bmenu_myfriend,R.drawable.bmenu_add,R.drawable.bmenu_browseproducts};
    private List<ProductInfo> mProductInfos = new ArrayList<ProductInfo>();
    MainGridAdapter mGridAdapter = new MainGridAdapter();
    MainListAdapter mListAdapter = new MainListAdapter();
    GridView gridView;
    ListView listView;
    int[] menuids = {R.id.rlMyFriend,R.id.rlMyWants,R.id.rlMyFollowers,R.id.rlMyFollowing,R.id.rlMyProfile,R.id.rlSettings,R.id.rlFeedBack};
    View[] menuViews = new RelativeLayout[menuids.length];
    LayoutInflater inflater ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("------>>" + System.currentTimeMillis());
        test();
        inflater = LayoutInflater.from(this);
        setContentView(inflater.inflate(R.layout.horz_scroll_with_image_menu, null));
        initFunctionMenus();
        scrollView = (MyHorizontalScrollView) findViewById(R.id.myScrollView);
        menu = findViewById(R.id.menu);
        loadContentList();
//        sysout
        
    }

    private void test() {
//    	Map<String, String> para = new HashMap<String, String>();
//    	para.put("userid", "001");
//    	para.put("username", "test");
//    	para.put("mobile", "13402045676");
//    	System.out.println(Url.getParams( para));
//		// TODO Auto-generated method stub
//    	String url = Url.url + Url.url_register;
//		SpecialRequest request = new SpecialRequest(url);
//		
//		request.postParams = "userid=001&password=111111&username=test&mobile=13402045676&auth_method=" 
//			+ Url.POST + "&auth_key=" + Url.AUTH_KEY + "&auth_nonce=" + nounce + "&auth_timestamp=" + tsp
//			+ "&auth_version=" + Url.AUTH_VERSION + "&auth_signature="+Url.getAuthParam(Url.url_register,tsp,nounce);
//		System.out.println("params:" + request.postParams);
//		request.encoding = "utf-8"; 
//		mHttpHandler.sendRequest(request); 
	}

//	private void loadContentGrid() {
//		app = inflater.inflate(R.layout.horz_scroll_app, null);
//		gridView = (GridView) app.findViewById(R.id.list);
//        initGridView();
//        initTitleBar();
//        mTitleBar = (GTitleBar) app.findViewById(R.id.mTitleBar);
//        mTitleBar.initLeftButton("", R.drawable.titlebar_menu, new HorzScrollWithListMenu.ClickListenerForScrolling(scrollView, menu));
//        mTitleBar.initRightButton("", R.drawable.titlebar_addfriend, this);
//        mTitleBar.initTitle("麦友");
//        View transparent = new TextView(this);
//        transparent.setBackgroundColor(android.R.color.transparent);
//        final View[] children = new View[] { transparent, app };
//        int scrollToViewIdx = 1;
//        scrollView.initViews(children, scrollToViewIdx, new HorzScrollWithListMenu.SizeCallbackForMenu(mTitleBar.getLeftButton()));
//        initBottomMenu();
//	}
	
	private void loadContentList() {
		app = inflater.inflate(R.layout.horz_scroll_app, null);
		listView = (ListView) app.findViewById(R.id.list);
		gridView = (GridView) app.findViewById(R.id.grid);
		gridView.setVisibility(View.GONE);
        initListView();
        initGridView();
        initTitleBar();
        mTitleBar = (GTitleBar) app.findViewById(R.id.mTitleBar);
        mTitleBar.initLeftButton("", R.drawable.titlebar_menu, new HorzScrollWithListMenu.ClickListenerForScrolling(scrollView, menu));
        mTitleBar.initRightButton("", R.drawable.titlebar_addfriend, this);
        mTitleBar.initTitle("麦友");
        View transparent = new TextView(this);
        //transparent.setBackgroundColor(android.R.color.transparent);
        final View[] children = new View[] { transparent, app };
        int scrollToViewIdx = 1;
        scrollView.initViews(children, scrollToViewIdx, new HorzScrollWithListMenu.SizeCallbackForMenu(mTitleBar.getLeftButton()));
        initBottomMenu();
	}
    
	

	private void initFunctionMenus() {
		
	}
	private void initBottomMenu() {
		// TODO Auto-generated method stub
		mBottomMenu = (BottomMenu) app.findViewById(R.id.mBottomMenu);
		mBottomMenu.setBackGroundResource(bmenu_drawables);
		mBottomMenu.setOnClickListener(this);
	}
	private void initTitleBar() {
		// TODO Auto-generated method stub
		for(int i=0;i<menuids.length;i++){
			menuViews[i] = findViewById(menuids[i]);
			menuViews[i].setOnClickListener(this);
		}
	}
	@Override
	public void onClick(View v) {
		Intent intent = new Intent();
		switch(v.getId()){
		
		case R.id.bmenu_leftButton:
			System.out.println("leftbutton");
			break;
		case R.id.bmenu_rightButton:
			System.out.println("right");
			
			break;
		case R.id.bmenu_middleButton:
			System.out.println("middle");
			setGoBack();
			intent.setClass(this, PostProductActivity.class);
			startActivity(intent);
			break;
		case R.id.rightButton:
			break;
		case R.id.rlMyFriend:
			mTitleBar.getLeftButton().performClick();
			listView.setVisibility(View.VISIBLE);
			gridView.setVisibility(View.GONE);
			break;
		case R.id.rlMyWants:
			mTitleBar.getLeftButton().performClick();
			listView.setVisibility(View.GONE);
			gridView.setVisibility(View.VISIBLE);
			break;
		case R.id.rlMyFollowers:
			mTitleBar.getLeftButton().performClick();
			listView.setVisibility(View.VISIBLE);
			gridView.setVisibility(View.GONE);
			break;
		case R.id.rlMyFollowing:
			mTitleBar.getLeftButton().performClick();
			listView.setVisibility(View.VISIBLE);
			gridView.setVisibility(View.GONE);
			break;
		case R.id.rlMyProfile:
			setGoBack();
			intent.setClass(this, MyProfileActivity.class);
			startActivity(intent);
			break;
		case R.id.rlSettings:
			break;
		case R.id.rlFeedBack:
			break;
		}
	}
	
	
	   public  void initGridView() {
	        // By using setAdpater method in listview we an add string array in list.
		   ProductInfo p1 = new ProductInfo();
		   p1.setName("iphone50");
		   p1.setPrice("5000");
		   ProductInfo p2 = new ProductInfo();
		   p2.setName("iphone4s");
		   p2.setPrice("4000");
		   mProductInfos.add(p1);
		   mProductInfos.add(p2);
		    gridView.setAdapter(mGridAdapter);
		    gridView.setOnItemClickListener(new OnItemClickListener() {
	            @Override
	            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
	                Context context = view.getContext();
	                String msg = "item[" + position + "]=" + parent.getItemAtPosition(position);
	                Toast.makeText(context, msg, 1000).show();
	                System.out.println(msg);
	                setGoBack();
	                Intent intent = new Intent(HorzScrollWithImageMenu.this,ProductDetailActivity.class);
	                startActivity(intent);
	            }
	        });
	    }
	   
	   private void initListView() {
		   // By using setAdpater method in listview we an add string array in list.
		   ProductInfo p1 = new ProductInfo();
		   p1.setName("iphone50");
		   p1.setPrice("5000");
		   ProductInfo p2 = new ProductInfo();
		   p2.setName("iphone4s");
		   p2.setPrice("4000");
		   mProductInfos.add(p1);
		   mProductInfos.add(p2);
		    listView.setAdapter(mListAdapter);
	        listView.setOnItemClickListener(new OnItemClickListener() {
	            @Override
	            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
	                Context context = view.getContext();
	                String msg = "item[" + position + "]=" + parent.getItemAtPosition(position);
	                Toast.makeText(context, msg, 1000).show();
	                System.out.println(msg);
	            }
	        });
		}
	    
	    
	    public  class MainGridAdapter  extends BaseAdapter{

			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return mProductInfos.size();
			}

			@Override
			public Object getItem(int position) {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public long getItemId(int position) {
				// TODO Auto-generated method stub
				return 0;
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				if(convertView==null){
					LayoutInflater inflater = LayoutInflater.from(HorzScrollWithImageMenu.this);
					convertView = inflater.inflate(R.layout.grid_item, null);
				}
				TextView tv1 = (TextView) convertView.findViewById(R.id.item_name);
				TextView tv2  = (TextView) convertView.findViewById(R.id.item_price);
				tv1.setText(mProductInfos.get(position).getName());
				tv2.setText(mProductInfos.get(position).getPrice());
				return convertView;
			}
	    	
	    }
	    
	    public  class MainListAdapter  extends BaseAdapter{

			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return mProductInfos.size();
			}

			@Override
			public Object getItem(int position) {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public long getItemId(int position) {
				// TODO Auto-generated method stub
				return 0;
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				if(convertView==null){
					LayoutInflater inflater = LayoutInflater.from(HorzScrollWithImageMenu.this);
					convertView = inflater.inflate(R.layout.grid_item, null);
				}
				TextView tv1 = (TextView) convertView.findViewById(R.id.item_name);
				TextView tv2  = (TextView) convertView.findViewById(R.id.item_price);
				tv1.setText(mProductInfos.get(position).getName());
				tv2.setText(mProductInfos.get(position).getPrice());
				return convertView;
			}
	    	
	    }



		@Override
		public void setGoBack() {
			Bundle bundle = new Bundle();
			bundle.putString("back2", getClass().getName());
			GoBack.goBackStack.add(bundle);
		}

		@Override
		protected void httpCompleted(ResponseInterface resp) {
			// TODO Auto-generated method stub
			System.out.println("---->" + ((SpecialResponse)resp).content);
		}

		@Override
		protected void initView() {
			// TODO Auto-generated method stub
			
		}

		@Override
		protected void getIntentData() {
			// TODO Auto-generated method stub
			
		}

		@Override
		protected void setIntentData() {
			// TODO Auto-generated method stub
			
		}
}
